﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;

using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Fitness;

namespace PolarImport.DeviceImporter
{
    class PolarSSeries : IFitnessDevice 
    {
        /// <summary>
        /// Constructor of the FitnessDevice. Set GUID, image, name and connection information.
        /// </summary>
        public PolarSSeries(Settings.PolarDevice Device)
        {
            this.id = Device.GUID;
            this.device = Device;
            this.name = "Polar " + Device.Modells; ;
            System.Resources.ResourceManager resourceManager = Properties.Resources.ResourceManager;
            this.image = (System.Drawing.Image)resourceManager.GetObject("Img_Polar_" + this.device.Monitor.ToString());

        } 
        
        #region IFitnessDevice Member

        public string Configure(string configurationInfo)
        {
            DeviceConfiguration prompt = new DeviceConfiguration(device);
            DeviceConfigurationInfo info = DeviceConfigurationInfo.Parse(configurationInfo);
            if (info.PolarDevice == 0)
                info.PolarDevice = this.device.Monitor ;
            prompt.ConfigInfo = info;
            if (prompt.ShowDialog() == DialogResult.OK)
            {
                return prompt.ConfigInfo.ToString();
            }
            return null;

        }

        public string ConfiguredDescription(string configurationInfo)
        {
            if (configurationInfo == null)
            {
                return this.Name;
            }
            DeviceConfigurationInfo info = DeviceConfigurationInfo.Parse(configurationInfo);
            return this.Name + Environment.NewLine + info.DeviceName + " : "
                                                   + info.PolarDevice.ToString() + " : "
                                                   + info.UploadConnection.ToString();
        }

        #endregion

        #region IDataImporter Member

        public Guid Id
        {
            get { return id; }
        }

        public System.Drawing.Image Image
        {
            get { return image; }
        }

        public bool Import(string configurationInfo, IJobMonitor monitor, IImportResults importResults)
        {
            // Parse the configurationInfo
            DeviceConfigurationInfo configInfo = DeviceConfigurationInfo.Parse(configurationInfo);

            
            // Set up the general information about the device the user
            // is trying to read from. Fill in the parameters for general information 
            // about the connection.

            PolarSSetMonitorInfo psmi = new PolarSSetMonitorInfo();
            psmi.iSize = Marshal.SizeOf(psmi);

            PolarSSetGeneral psg = new PolarSSetGeneral();
            psg.iSize = Marshal.SizeOf(psg);
            
            psg.iConnection = ConnectionMethod.HRMCOM_CONNECTION_IR;
            psg.iMonitorID = 0;
            psg.bConnectionDlg = true;
            psg.bNoCreateWave = true;
            psg.bNoDeleteWave = true;
            psg.bNoPlayWave = true;
            psg.bLoadFromDataFile = false;
            psg.bSaveAsDataFile = false;
            psg.szWaveFile = null;
            psg.szDlgMsg = Properties.Resources.PolarImport_General_ReadMonitorInfo;
            psg.bFixErrors = true;
            psg.iParam = 0;
            psg.lParam = 0;

            // Read Monitor inforamtions
            bool test = PolarImportJobs.ReadMonitorInfoIR(psmi, psg, configInfo.DeviceName , monitor );
            if (!test)
            {
                return false;
            }
            // TODO: What if the selected monitortype doesn't match the one connected...
            string MonitorName = PolarHRM.GetHRMonitorName(psmi.iMonitorInUse, psmi.iMonitorSubModel);

            // Check if there are exercises to import
            int TotalFiles = psmi.iTotalFiles;
            if (TotalFiles == 0)
            {
                monitor.ErrorText = Properties.Resources.PolarImport_Import_NoFitnessFileFound;
                
                return false; // No recorded files on the watch
            }

            // Calculate the used memory
            PolarMemory deviceMem = new PolarMemory(psmi);
            
            // First info for the user:
            string FirstStatus = MonitorName + Environment.NewLine +
                                 String.Format(Properties.Resources.PolarImport_Import_FitnessFileCount + Environment.NewLine, TotalFiles) +
                                 String.Format(Properties.Resources.PolarImport_Import_MemoryUsage + Environment.NewLine, deviceMem.UsedMem, deviceMem.TotalMem );

            monitor.StatusText = FirstStatus;
            monitor.PercentComplete = deviceMem.UsedMemPercentage ;

            // Workflow
            // Basic Infos
            if (configInfo.WriteBasicValues)
            { 
            
            }
            // TimeBasedInfos
            if (configInfo.WriteTimeBasedValues)
            { 
            
            }
            // LapData
            if (configInfo.WriteLapData)
            { 
            
            }
            // Detailed Infos
            if (configInfo.WriteDetailedInfos)
            { 
            
            }

            return false;
        }

        public string Name
        {
            get { return name; }
        }

        #endregion

        #region Private members

        /// <summary>The unique GUID of this FitnessDevice</summary>
        private Guid id;

        /// <summary>The image to represent this FitnessDevice</summary>
        private System.Drawing.Image image;

        /// <summary>The name of this FitnessDevice</summary>
        private string name;

        private Settings.PolarDevice device;

        #endregion
    }
}
